package com.li.forum.service.impl;

import com.li.forum.common.AppResult;
import com.li.forum.common.ResultCode;
import com.li.forum.dao.BoardMapper;
import com.li.forum.exception.ApplicationException;
import com.li.forum.model.Board;
import com.li.forum.service.IBoardService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BoardServiceImpl implements IBoardService {
    
    @Resource
    private BoardMapper boardMapper;
    
    private static final Logger log = LoggerFactory.getLogger(BoardServiceImpl.class);
    
    @Override
    public List<Board> selectByNum(Integer num) {
        if(num<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询数据
        return boardMapper.selectByNum(num);
    }
    
    @Override
    public void addOneArticleCountById(Long id) {
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        
        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子
        Board updataBoard = new Board();
        updataBoard.setId(board.getId());
        updataBoard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updataBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
    
    @Override
    public void subOneArticleCountById(Long id) {
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        
        //更新帖子
        Board updataBoard = new Board();
        updataBoard.setId(board.getId());
        updataBoard.setArticleCount(board.getArticleCount() - 1);
        //判断帖子不能为负数
        if(updataBoard.getArticleCount() < 0){
            updataBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updataBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
    
    @Override
    public Board selectById(Long id) {
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 调用DAO查询数据
        Board board = boardMapper.selectByPrimaryKey(id);
        // 返回结果
        return board;
    }
    
   
}
